//
//  ShipinController.m
//  网易新闻
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "ShipinController.h"
#import "ShipinCell.h"
#import "MJRefresh.h"
#import "AFTool.h"
#import "ShipinModel.h"
#import "BBSController.h"
#import "Settings.h"
@interface ShipinController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@end
@implementation ShipinController
static int count = 0;
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        [self createTableView];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewWillLayoutSubviews{
    self.tableView.contentOffset = CGPointMake(0, 0);
    [self.tableView.header beginRefreshing];    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    [self notificationHandle];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 565) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    [self.tableView registerClass:[ShipinCell class] forCellReuseIdentifier:@"ShipinCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        count = 0;
        [self handelDataWithCount:count];
        [self.tableView.header endRefreshing];
    }];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        count += 10;
        [self handelDataWithCount:count];
        [self.tableView.footer endRefreshing];
    }];
}

- (void)handelDataWithCount:(int)count{
    NSString *urlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/home/%d-10.html", count];
    [AFTool GETWithURL:urlStr body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if (count == 0) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"videoList"]];
            [[Settings shareSettings]writeToDBWithDic:tempDic WithName:urlStr];
        }else{
            [self.modArr addObjectsFromArray:[tempDic objectForKey:@"videoList"]];
        }
        [tempDic release];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:[[Settings shareSettings]dicSelectFromDB:urlStr]];
        if (![tempDic isEqual:nil]) {
            self.modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:@"videoList"]];
            [tempDic release];
            [self.tableView reloadData];
        }
        NSLog(@"404(ShipinController)");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShipinModel *mod = [ShipinModel modWithDic:self.modArr[indexPath.row]];
    ShipinCell *cell = [[ShipinCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ShipinCell"];
    cell.mod = mod;
    self.cellheight = cell.height;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShipinModel *mod = [ShipinModel modWithDic:self.modArr[indexPath.row]];
    BBSController *bbs = [[BBSController alloc]init];
    bbs.replyBoard = mod.replyBoard;
    bbs.docid = mod.replyid;
    [self.navigationController pushViewController:bbs animated:YES];
    [bbs release];
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    }];
}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_modArr release];
    [_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
